﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;

using TableTop.Presentation;

namespace TableTop.Presentation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : NavigationWindow 
    {
        private bool bFirstAddonUpdateRun = false;
        private Button btnHomeButton = null;
        private Button btnPlayOnlineButton = null;

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            DataHelper.Addons.Updating += new EventHandler<DataManagement.Addon.AddonUpdateEventArgs>(Addons_Updating);
        }        

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.LocateControls();
            this.RegisterControlsEvents();
        }

        /// <summary>
        /// Locate template controls
        /// </summary>
        private void LocateControls()
        {
            this.btnHomeButton = this.Template.FindName("btnHomeButton", this) as Button;
            this.btnPlayOnlineButton = this.Template.FindName("btnPlayOnlineButton", this) as Button;
        }

        /// <summary>
        /// Register template controls events
        /// </summary>
        private void RegisterControlsEvents()
        {
            if (this.btnHomeButton != null)
            {
                this.btnHomeButton.Command = NavigationCommands.BrowseHome;
                this.btnHomeButton.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseHome,
                                                                          new ExecutedRoutedEventHandler(this.BrowseHome_Execute),
                                                                          new CanExecuteRoutedEventHandler(this.BrowseHome_CanExecute)));
            }
            else
                System.Diagnostics.Debug.WriteLine("btnHomeButton is null. Cannot locate over template");
                                                                          
            
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.bFirstAddonUpdateRun == false)
            {
                this.bFirstAddonUpdateRun = true;
                DataHelper.Addons.Update();
            }
        }

        private void Addons_Updating(object sender, DataManagement.Addon.AddonUpdateEventArgs e)
        {
            String strDebugLog = String.Empty;

            strDebugLog = String.Format("Received addon update action {0}, addon checked {1}/{2}",
                                        e.Action.ToString(),
                                        e.Checked,
                                        e.CheckCount);
            System.Diagnostics.Debug.WriteLine(strDebugLog);
        }        

        private void BrowseHome_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void BrowseHome_Execute(object sender, ExecutedRoutedEventArgs e)
        {
            this.Navigate(VisualHelper.Pages.Home);
        }
    }
}
